from rest_framework.renderers import JSONRenderer


# 导入控制返回的JSON格式的类
class CustomRenderer(JSONRenderer):
    msg = ''
    code = 200

    # 重构render方法
    def render(self, data, accepted_media_type=None, renderer_context=None):
        if renderer_context:
            # 判断实例的类型，返回的数据可能是列表也可能是字典
            if isinstance(data, dict):
                # 如果是字典的话应该是返回的数据，会包含msg,code,status等字段必须抽离出来
                if renderer_context['response'].status_code != 200:
                    self.code = renderer_context['response'].status_code
                    self.msg = data.get('detail') if data.get('detail') else data.pop('msg', '操作失败')
                    ret = {
                        'msg': self.msg,
                        'code': self.code
                    }
                    return super().render(ret, accepted_media_type, renderer_context)
                else:
                    self.msg = data.pop('msg', '操作成功')
                    self.code = data.pop('code', 200)
                    # 重新构建返回的JSON字典
                    if 'status' in data.keys():
                        del data['status']
                        data = data['data']
                    else:
                        data = data
                    # 自定义返回数据格式
                    ret = {
                        'msg': self.msg,
                        'code': self.code,
                        'data': data,
                    }
                    # 返回JSON数据
                    return super().render(ret, accepted_media_type, renderer_context)
            else:
                # 自定义返回数据格式
                ret = {
                    'msg': self.msg if self.msg else "操作成功",
                    'code': self.code,
                    'data': data,
                }
                # 返回JSON数据
                return super().render(ret, accepted_media_type, renderer_context)
        else:
            return super().render(data, accepted_media_type, renderer_context)
